package com.lovelyj.trip_by_train.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovelyj.trip_by_train.entity.Coach;
import com.lovelyj.trip_by_train.entity.DailyTrain;
import com.lovelyj.trip_by_train.entity.Seat;
import com.lovelyj.trip_by_train.mapper.CoachMapper;
import com.lovelyj.trip_by_train.service.CoachService;
import com.lovelyj.trip_by_train.service.SeatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements CoachService {

    @Autowired
    private SeatService seatService;

    @Autowired
    private CoachMapper coachMapper;


//    @Override
//    @Transactional
//    public void saveWithSeats(Coach coach) {
//        log.info("saveWithSeats..."+coach);
//        List<Seat> seats=new ArrayList<>();
//        for(int i=0;i<coach.getSeatRowNumber();i++){
//            for(int j=0;j<coach.getSeatColNumber();j++){
//                Seat seat=new Seat();
//                seat.setSeatRow(i);
//                seat.setSeatCol(j);
//                seat.setTrainNum(coach.getTrainNum());
//                seat.setCoachNum(coach.getCoachNum());
//                seats.add(seat);
//            }
//        }
//        this.save(coach);
//        seatService.saveBatch(seats);
//    }


    @Override
    @Transactional
    public int saveWithSeats(Coach coach) {
        log.info("saveWithSeats..."+coach);
        Integer result= 0;
        coachMapper.saveWithSeatsAndTickets(coach.getTrainNum(), coach.getCoachNum(), coach.getSeatRowNumber(),coach.getSeatColNumber(),result);
        return result;
    }



    @Override
    @Transactional
    public void updateWithSeats(Coach coach){
        log.info("updateWithSeats..."+coach);
        List<Seat> seats=new ArrayList<>();
        LambdaQueryWrapper<Seat> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Seat::getTrainNum,coach.getTrainNum());
        lambdaQueryWrapper.eq(Seat::getCoachNum,coach.getCoachNum());
        seatService.remove(lambdaQueryWrapper);
        for(int i=0;i<coach.getSeatRowNumber();i++){
            for(int j=0;j<coach.getSeatColNumber();j++){
                Seat seat=new Seat();
                seat.setSeatRow(i);
                seat.setSeatCol(j);
                seat.setTrainNum(coach.getTrainNum());
                seat.setCoachNum(coach.getCoachNum());
                seats.add(seat);
            }
        }
        this.updateById(coach);
        seatService.saveBatch(seats);
    }

    @Override
    @Transactional
    public void deleteByIdsWithSeats(List<Long> ids){
        log.info("deleteByIdsWithSeats..."+ids);
        for(Long id:ids){
            deleteByIdWithSeats(id);
        }
    }

    @Override
    @Transactional
    public void deleteByIdWithSeats(Long id){
        log.info("deleteByIdWithSeats..."+id);
        Coach coach=this.getById(id);
        LambdaQueryWrapper<Seat> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Seat::getTrainNum,coach.getTrainNum());
        lambdaQueryWrapper1.eq(Seat::getCoachNum,coach.getCoachNum());
        seatService.remove(lambdaQueryWrapper1);
        this.removeById(id);
    }

}
